package listener;

import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;

/**
 * 实现了 ServletRequestListener 的接口的类
 * 就是监听Request创建或销毁的监听器
 * 可以用来监控某个IP访问网站的频率，日志记录访问资源
 */
public class LmyServletRequestListener implements ServletRequestListener {

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        System.out.println("LmyServletRequestListener 监听到request对象被创建...");
        ServletRequest servletRequest = sre.getServletRequest();
        System.out.println("记录访问日志...");
        System.out.println("访问IP=" + servletRequest.getRemoteAddr());
        System.out.println("访问资源=" + ((HttpServletRequest) servletRequest).getRequestURL());
    }

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        System.out.println("LmyServletRequestListener 监听到request对象被销毁...");
    }


}
